package org.bes.utils;

import org.bes.WinClass.FileManager;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.bes.WinClass.VersionControl.versionFileMap;

public class StrPathUtils {
    // 正则表达式，用于提取版本号（例如：V2.0.0，V1.2.3等）
    public static final Pattern VERSION_PATTERN = Pattern.compile("V(\\d+\\.\\d+\\.\\d+)");

    //get the index of the last dot in a string
    public static int getDotIndex(String str) {
        return str.lastIndexOf(".");
    }


    //get the extension of a file
    public static String getExtension(String str) {
        int dotIndex = getDotIndex(str);
        if (dotIndex == -1) {
            return "";
        }
        return str.substring(dotIndex + 1);
    }



    public static Path getFileNameWithoutExtension(Path filePath) {
        int dotIndex = StrPathUtils.getDotIndex(filePath.getFileName().toString());
        return Path.of(filePath.getFileName().toString().substring(0, dotIndex));
    }

    public static Path stripFirstPart(Path filePath){

        Path pathWithoutFirstPart = filePath.subpath(1, filePath.getNameCount());
        return pathWithoutFirstPart;
    }


    public static Path getRelavtivePath(File file){
        return stripFirstPart(FileManager.getBackupDir().relativize(file.toPath()));
    }
    //public static Path getOriginPath(Path path) {
    //    return path.getParent().resolve(path.getFileName().toString().substring(0, getDotIndex(path.getFileName().toString())));
    //}
    public static String getFileVersion(File file) {
        // 获取文件的绝对路径
        String filePath = file.getAbsolutePath();

        // 使用正则匹配版本号
        Matcher matcher = VERSION_PATTERN.matcher(filePath);

        // 如果找到了匹配的版本号
        if (matcher.find()) {
            // 提取版本号（例如：V2.0.0 -> 2.0.0）
            String version = matcher.group(1);
            return version;
        } else {
            // 如果没有匹配的版本号，打印提示信息

            System.out.println("No version found in file path: " + filePath);
            return null;
        }
    }



}
